/** * Discard draft changes - reverts to live version * * POST /_emdash/api/content/{collection}/{id}/discard-draft */ import type { APIRoute } from "astro"; import { requireOwnerPerm } from "#api/authorize.js"; import { apiError, mapErrorStatus, unwrapResult } from "#api/error.js"; export const prerender = false; export const POST: APIRoute = async ({ params, locals, url, cache }) => { const { emdash, user } = locals; const collection = params.collection!; const id = params.id!; if (!emdash?.handleContentDiscardDraft || !emdash?.handleContentGet) { return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500); } const locale = url.searchParams.get("locale") || undefined; // Fetch item to check ownership const existing = await emdash.handleContentGet(collection, id, locale); if (!existing.success) { return apiError( existing.error?.code ?? "UNKNOWN_ERROR", existing.error?.message ?? "Unknown error", mapErrorStatus(existing.error?.code), ); } const existingData = existing.data && typeof existing.data === "object" ? // eslint-disable-next-line typescript/no-unsafe-type-assertion -- handler returns unknown data; narrowed by typeof check above (existing.data as Record) : undefined; // Handler returns { item, _rev } — extract the item for ownership check const existingItem = existingData?.item && typeof existingData.item === "object" ? // eslint-disable-next-line typescript/no-unsafe-type-assertion -- narrowed by typeof check above (existingData.item as Record) : existingData; const authorId = typeof existingItem?.authorId === "string" ? existingItem.authorId : ""; const denied = requireOwnerPerm(user, authorId, "content:edit_own", "content:edit_any"); if (denied) return denied; const resolvedId = typeof existingItem?.id === "string" ? existingItem.id : id; const result = await emdash.handleContentDiscardDraft(collection, resolvedId); if (!result.success) return unwrapResult(result); if (cache?.enabled) await cache.invalidate({ tags: [collection, resolvedId] }); return unwrapResult(result); };